{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f3324fc3",
   "metadata": {},
   "source": [
    "### 排序算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "68445921",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-21, -12, 5, 9, 36]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted([36,5,-12,9,-21])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4c55dd0",
   "metadata": {},
   "source": [
    "sorted函数也是一个高阶函数，它还可以接收一个key函数来实现自定义的排序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6bf809b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 9, -12, -21, 36]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted([36,5,-12,9,-21],key=abs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f4b96ac",
   "metadata": {},
   "source": [
    "对字符串的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "09f4d4aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Credit', 'Zoo', 'about', 'bob']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(['bob', 'about', 'Zoo', 'Credit'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df2fc572",
   "metadata": {},
   "source": [
    "如果要忽略大小写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "84f691cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['about', 'bob', 'Credit', 'Zoo']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(['bob', 'about', 'Zoo', 'Credit'],key=str.lower)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8fcefc8",
   "metadata": {},
   "source": [
    "如果要进行反向排序，传入第三个参数reverse=True即可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7c653f4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Zoo', 'Credit', 'bob', 'about']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(['bob', 'about', 'Zoo', 'Credit'],key=str.lower,reverse=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b90f7c5a",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a57208c1",
   "metadata": {},
   "source": [
    "sorted是一个高阶函数，用sorted排序的关键在于实现一个映射函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b0d1651",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
